<?php
/**
 * 學期成績檔升級
 * @author cik
 *
 */
class Default_Model_Upgrade_StudSemeScoreNor extends Default_Model_Upgrade_Core
{
    protected $_table="stud_seme_score_nor";
    protected $_targetTable = array('student_score_comment');

    public function upgrade()
    {
        if ($this->isUpgrade())
        return 'isUpgrde';
        else {
            // 更改 資料表為 MYISAM
            $query = "ALTER TABLE `student_score_comment` DROP FOREIGN KEY `student_score_comment_ibfk_1`";
            $this->_newdbPdo->exec($query) ;
             $query = "ALTER TABLE `student_score_comment`  ENGINE = MYISAM";
            $this->_newdbPdo->exec($query);


            $query = "SELECT * FROM stud_seme_score_nor";
            $res = $this->_sfs3Pdo->query($query)->fetchAll(PDO::FETCH_ASSOC);
            foreach($res as $row) {
                $arr = array();

                $year=  (int)substr($row['seme_year_seme'],0,-1);
                $semester = (int)substr($row['seme_year_seme'],-1);
                $arr['student_sn'] = $row['student_sn'];
                $arr['year'] = $year;
                $arr['semester'] = $semester;
                $arr['score_type'] = $row['ss_id'];
                $arr['score'] = $row['ss_score'];
                $arr['comment'] = $this->iconv($row['ss_score_memo']);
                $this->fromArray('student_score_comment',$arr);
            }

               // 加入 FOREIGN KEY
            $query = "ALTER TABLE `student_score_comment`  ENGINE = INNODB";
            $this->_newdbPdo->exec($query);
            $query = "ALTER TABLE `student_score_comment`
  ADD CONSTRAINT `student_score_comment_ibfk_1` FOREIGN KEY (`student_sn`) REFERENCES `student` (`sn`) ON DELETE CASCADE ON UPDATE CASCADE;
         ";
            $this->_newdbPdo->exec($query) ;

            $log['memo'] = 'stud_seme_score_nor  升級';
            $log['upgrade_time']    = date("Y-m-d h:i:s");

            $this->finishUpgrade($log);
        }
    }

}